// pages/my/addTheAddress/addTheAddress.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    positionMsg:{
      name:'',
      phonenumber: '',
      detailed: '',
      position: '',
      chenkedIndex: 0,
    },
    checkBool:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  //地址编辑
  onLoad: function(options) {
    let id = options.id;
    if(id){
      this.setData({
        id: id,
      });
    }else{
      return;
    }
    console.log(id);
    let self = this;
    let address = self.data.position;
    if (self.data.id != '') {
      app.util.ajax({
        url: 'api/Address/addressDetail',
        data: {
          id: self.data.id,
        }
      }, function (res) {
        let positionMsg = res.data.data,
            isCheck = positionMsg.is_default == '1' ? 'true' : ''; 
        self.setData({
          positionMsg: {
            name: positionMsg.consignee,
            phonenumber: positionMsg.phone,
            detailed: positionMsg.detail,
            position: positionMsg.area_info,
            chenkedIndex: positionMsg.is_default,

          },
          checkBool:isCheck
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  
  //电话value
  telephone: function(e) {
    let self = this,
      value = e.detail.value,
      itemMsg = this.data.positionMsg;
    itemMsg.phonenumber = value;
    this.setData({
      positionMsg:itemMsg
    })
  },

  //姓名value
  writeName: function(e){
    let self = this,
      write = e.detail.value,
      itemMsg = self.data.positionMsg;
      itemMsg.name = write;
    self.setData({
      positionMsg: itemMsg,
    })
  },

  //详细地址value
  minute: function(e){
    let self = this,
      minuteValue = e.detail.value,
      itemMsg = self.data.positionMsg;
    itemMsg.detailed = minuteValue;
    self.setData({
      positionMsg: itemMsg,
    })
  },

  //选择地址
  getLocation: function(e){
    let self = this;
    wx.chooseLocation({
      success: function(res) {
        let location = res.name,
            positionMsg = self.data.positionMsg;
        positionMsg['position'] = location;
        self.setData({
          positionMsg: positionMsg
        })
      },
    })
  },

  //默认地址
  choiceChenked: function(e){
    let positionMsg = this.data.positionMsg,
        isDefault = positionMsg.chenkedIndex;
    isDefault = isDefault == 0 ? 1 : 0;
    positionMsg.chenkedIndex = isDefault;
    this.setData({
      positionMsg:positionMsg
    })
  },


  //保存
  submitPreserve: function(e){
    let self = this,
      id = self.data.id,
      region = self.data.positionMsg.position,
      chenkedIndex = self.data.positionMsg.chenkedIndex,
      name = e.detail.value.name,
      detailed = e.detail.value.explicit,
      phone = e.detail.value.phone;
    if (phone == "") {
      wx.showToast({
        title: '电话不能为空',
        icon: 'none'
      })
    } else {
      var mobile = /^[1][3,4,5,7,8][0-9]{9}$/;
      var isMobile = mobile.test(phone);
      //输入有误的话，弹出模态框提示         
      if (!isMobile) {
        wx.showToast({
          title: '电话号码有误！',
          icon:'none'
        });
        return 
      }
    }

    if (name == '' || region == '' || detailed == '' || phone == ''){
      wx.showToast({
        title: '内容不能为空',
        icon: 'none'
      })
     }else{
       app.util.ajax({
         url: "api/Address/addressSave",
         method: 'POST',
         data: {
           consignee: name,
           area_info: region,
           detail: detailed,
           phone: phone,
           is_default: chenkedIndex,
           address_id: id,
         }
       },function(res){
       })
      app.util.pageJump('/pages/my/addressManagement/addressManagement',);
     }
  },
  
})